using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class attach : Feathertail.Command
    {
        private FeathertailClient _Client;

        private Dictionary<string, ObjectManager.AttachmentPoint> AttachmentPtNames = new Dictionary<string, ObjectManager.AttachmentPoint>();

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
            AttachmentPtNames["Default"] = 0;
            AttachmentPtNames["Chest"] = ObjectManager.AttachmentPoint.Chest;
            AttachmentPtNames["Chin"] = ObjectManager.AttachmentPoint.Chin;
            AttachmentPtNames["LeftEar"] = ObjectManager.AttachmentPoint.LeftEar;
            AttachmentPtNames["LeftEyeball"] = ObjectManager.AttachmentPoint.LeftEyeball;
            AttachmentPtNames["LeftFoot"] = ObjectManager.AttachmentPoint.LeftFoot;
            AttachmentPtNames["LeftForearm"] = ObjectManager.AttachmentPoint.LeftForearm;
            AttachmentPtNames["LeftHand"] = ObjectManager.AttachmentPoint.LeftHand;
            AttachmentPtNames["LeftHip"] = ObjectManager.AttachmentPoint.LeftHip;
            AttachmentPtNames["LeftLowerLeg"] = ObjectManager.AttachmentPoint.LeftLowerLeg;
            AttachmentPtNames["LeftPec"] = ObjectManager.AttachmentPoint.LeftPec;
            AttachmentPtNames["LeftShoulder"] = ObjectManager.AttachmentPoint.LeftShoulder;
            AttachmentPtNames["LeftUpperArm"] = ObjectManager.AttachmentPoint.LeftUpperArm;
            AttachmentPtNames["LeftUpperLeg"] = ObjectManager.AttachmentPoint.LeftUpperLeg;
            AttachmentPtNames["RightEar"] = ObjectManager.AttachmentPoint.RightEar;
            AttachmentPtNames["RightEyeball"] = ObjectManager.AttachmentPoint.RightEyeball;
            AttachmentPtNames["RightFoot"] = ObjectManager.AttachmentPoint.RightFoot;
            AttachmentPtNames["RightForearm"] = ObjectManager.AttachmentPoint.RightForearm;
            AttachmentPtNames["RightHand"] = ObjectManager.AttachmentPoint.RightHand;
            AttachmentPtNames["RightHip"] = ObjectManager.AttachmentPoint.RightHip;
            AttachmentPtNames["RightLowerLeg"] = ObjectManager.AttachmentPoint.RightLowerLeg;
            AttachmentPtNames["RightPec"] = ObjectManager.AttachmentPoint.RightPec;
            AttachmentPtNames["RightShoulder"] = ObjectManager.AttachmentPoint.RightShoulder;
            AttachmentPtNames["RightUpperArm"] = ObjectManager.AttachmentPoint.RightUpperArm;
            AttachmentPtNames["RightUpperLeg"] = ObjectManager.AttachmentPoint.RightUpperLeg;
            AttachmentPtNames["Mouth"] = ObjectManager.AttachmentPoint.Mouth;
            AttachmentPtNames["Nose"] = ObjectManager.AttachmentPoint.Nose;
            AttachmentPtNames["Pelvis"] = ObjectManager.AttachmentPoint.Pelvis;
            AttachmentPtNames["Skull"] = ObjectManager.AttachmentPoint.Skull;
            AttachmentPtNames["Spine"] = ObjectManager.AttachmentPoint.Spine;
            AttachmentPtNames["Stomach"] = ObjectManager.AttachmentPoint.Stomach;
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length < 2)
            {
                return GetHelpText();
            }

            string output = "";

            if (CmdLine[1].ToLower().Equals("/list"))
            {
                output += "Known attachment points:" + Environment.NewLine;
                foreach (string pt in AttachmentPtNames.Keys)
                {
                    output += pt + ", ";
                }

                output.Remove(output.Length - 2);
                return output;
            }

            string item2rez = CmdLine[1];
            ObjectManager.AttachmentPoint attachPtCode = ObjectManager.AttachmentPoint.Default;
            string attachPtName = "Default";

            if (CmdLine.Length == 3)
            {
                foreach (KeyValuePair<string, ObjectManager.AttachmentPoint> kvp in AttachmentPtNames)
                {
                    if (kvp.Key.ToLower().Equals(CmdLine[2].ToLower()))
                    {
                        attachPtCode = kvp.Value;
                        attachPtName = kvp.Key;
                        break;
                    }
                }
            }
            else
            {
                output += "No attachment point specified." + Environment.NewLine;
            }

            // Iterate through current folder contents to find item
            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

            output += "Looking for : " + item2rez + Environment.NewLine;
            iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.ItemID.Equals(item2rez) || item.Name.Equals(item2rez))
                    {
                        output += "Attempting to attach " + item.Name + " to " + attachPtName;
                        item.Attach(attachPtCode);
                    }
                }
            }

            return output;

        }

        public string[] GetCommandStrings()
        {
            return new string[] { "attach" };
        }

        public string GetSummaryText()
        {
            return "Attempt to attach an inventory item.";
        }

        public string GetHelpText()
        {
            string output = "Usage: attach [item] [optional:point]" + Environment.NewLine;
            output += "List points: attach /list";
            return output;
        }

        #endregion
    }
}
